package com.huanletao.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.Type;
import java.util.Arrays;

/**
 * <p>
 * </p>
 * package: com.huanletao.annotation
 * description:
 * className: App
 * author:  踏雪无痕
 * copyright: Copyright (c) 2022/8/24 14:58
 * since: 1.0.0
 */
public class App {
    public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        Class<Student> studentClass = Student.class;

        Demo annotation = AnnotationUtils.findAnnotation(studentClass, Demo.class);
        System.out.println("annotation = " + annotation);

        Method say = studentClass.getMethod("say", String.class);

        Demo annotation1 = AnnotationUtils.findAnnotation(say, Demo.class);
        System.out.println("annotation1 = " + annotation1);



        Annotation[][] parameterAnnotations = say.getParameterAnnotations();
        for (Annotation[] parameterAnnotation : parameterAnnotations) {
            System.out.println(Arrays.toString(parameterAnnotation));
        }

        Parameter[] parameters = say.getParameters();
        for (Parameter parameter : parameters) {
            String name = parameter.getName();
            System.out.println("name = " + name);
            Type parameterizedType = parameter.getParameterizedType();

            String typeName = parameterizedType.getTypeName();
            System.out.println("typeName = " + typeName);
            Class<String> aClass = (Class<String>) Class.forName(typeName);
            String s = aClass.newInstance();
            System.out.println("s = " + s);
            Demo annotation2 = AnnotationUtils.findAnnotation(parameter, Demo.class);
            System.out.println("annotation2 = " + annotation2);
        }

        Field name = studentClass.getDeclaredField("name");
        Demo annotation2 = AnnotationUtils.findAnnotation(name, Demo.class);
        System.out.println("annotation2 = " + annotation2);
    }
}
